package com.hex.campus.backend.hex_campus_backend.service;

import com.hex.campus.backend.hex_campus_backend.entity.Book;
import com.hex.campus.backend.hex_campus_backend.repository.BookRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class BookService {

    @Autowired
    private BookRepository bookRepository;

    public List<Book> getAllBooks() {
        return bookRepository.findAll();
    }

    public Book addBook(Book book) {
        return bookRepository.save(book);
    }

    public List<Book> searchBooks(String keyword) {
        return bookRepository.findByTitleContaining(keyword);
    }

    public List<Book> getBooksByUserId(Long userId) {
        return bookRepository.findByUserId(userId); // 查询用户发布的教材
    }

    public void deleteBook(Long bookId) {
        bookRepository.deleteById(bookId); // 删除教材
    }
}